/**
 * @file    gt98xx_drv_pwm.c
 * @author  Giantec-Semi ATE
 * @brief   Source file of SPI DRV module.
 * @version 0.1
 * 
 * @copyright Copyright (c) 2021 Giantec-Semi
 */

#include "gt98xx_drv_pwm.h"

/**
 * @addtogroup GT98xx_DRV
 * @{
 */

/**
 * @addtogroup PWM_DRV
 * @{
 */

/**
 * @addtogroup PWM_DRV_Private_Macros PWM Drivers Private Macros
 * @{
 */

void DrvPiezo_Init(void)
{
		uint32_t updrdy;
		
		WRITE_REG(PWM->PWM_CFG,0x000000FF);		 //ch0~ch3 PIEZO MODE,OTHERS DISABLE
		WRITE_REG(PWM->PWM_CR,0x00007F00);     //stop ocp,VCM,4PIZ,STOP&RST SMA
		WRITE_REG(PWM->PWM_IR,0x00000000);     // OC INT FLAG/clear & PIZ_stop int FLAG/clear
		WRITE_REG(PWM->PWM_CHCFG,0x0000BBBB);	//CH0~CH3: H->L
		WRITE_REG(PWM->PWM_CH0CG,0xFFFF023B);//duty cycle of ch0:high level time= 0x023B/96/2=2.98us,low level time=T-2.98us
		WRITE_REG(PWM->PWM_CH1CG,0x023B0000);//duty cycle of ch1:low level time=0x023B0/96/2=2.98us,high level time=T-2.98us
		WRITE_REG(PWM->PWM_CH2CG,0xFFFF023B);//duty cycle of ch2:high level time= 0x023B/96/2=2.98us,low level time=T-2.98us
		WRITE_REG(PWM->PWM_CH3CG,0x023B0000);//duty cycle of ch3:low level time=0x023B0/96/2=2.98us,high level time=T-2.98us
		WRITE_REG(PWM->PZ_CFG,0x00000000);
		WRITE_REG(PWM->PZ0_CFG,0x36B002CA);//ch0~ch1:pulse count=0x36B0,frequency of PWM:T=0x02CA/96/2=3.72us,f=1/T=269K
		WRITE_REG(PWM->PZ1_CFG,0x36B002CA);//ch2~ch3:pulse count=0x36B0,frequency of PWM:T=0x02CA/96/2=3.72us,f=1/T=269K
	
		updrdy = READ_REG(PWM->PWM_STA)& 0x00010000; 
		while(updrdy != 0x00010000) {
			updrdy = READ_REG(PWM->PWM_STA) & 0x00010000;	
		}
		
		WRITE_REG(PWM->PWM_CR, 0x00010000);		//Update configurable register	
		WRITE_REG(PWM->PWM_CR,0x00000006);//start PIEZO0~1
}

void DrvVCM_Init(void)
{
		uint32_t updrdy;
		WRITE_REG(PWM->PWM_CFG,0x0000000F);		 //ch0/1 PIZEO MODE,OTHERS DISABLE
		WRITE_REG(PWM->PWM_CR,0x00007F00);     //stop ocp,VCM,4PIZ,STOP&RST SMA
		WRITE_REG(PWM->PWM_IR,0x00000000);     // OC INT FLAG/clear & PIZ_stop int FLAG/clear
		//WRITE_REG(PWM->PWM_OC_CFG,0x1);				//OC int enable	
		WRITE_REG(PWM->PWM_CHCFG,0x000000EE);	//CH0,CH1: H->L,CH2:L->H

		//WRITE_REG(MISC->OCP_REG,0x7c3);				//OCP enable	
		
		WRITE_REG(PWM->PWM_CH0CG,0xFFFF040A);		
		WRITE_REG(PWM->PWM_CH1CG,0x040A0000);		
		
		WRITE_REG(PWM->PZ_CFG,0x00000000);
		WRITE_REG(PWM->PZ0_CFG,0xFFFF0814);		
	
	
		updrdy = READ_REG(PWM->PWM_STA)& 0x00010000; 
		while(updrdy != 0x00010000) {
			updrdy = READ_REG(PWM->PWM_STA) & 0x00010000;	
		}
		
		WRITE_REG(PWM->PWM_CR, 0x00010000);		//Update configurable register	
		WRITE_REG(PWM->PWM_CR,0x00000002);
		//Delay(100);
		//WRITE_REG(PWM->PWM_CR,0x00000040);

	//WRITE_REG(PWM->PWM_CR,0x00000020);     //START VCM
}
void DrvPiezo_Config1(void)
{
		uint32_t updrdy,time;
		
		WRITE_REG(PWM->PWM_CH0CG,0xFFFF020A);		
		WRITE_REG(PWM->PWM_CH1CG,0xFFFF0000);		
		
		updrdy = READ_REG(PWM->PWM_STA)& 0x00010000; 
		while(updrdy != 0x00010000) {
			updrdy = READ_REG(PWM->PWM_STA) & 0x00010000;	
		}
		WRITE_REG(PWM->PWM_CR, 0x00010000);


		WRITE_REG(PWM->PZ0_CFG,0xFFFF0414);			 
		WRITE_REG(PWM->PWM_CR,0x0000001E);	 //START PIZ 
		//WRITE_REG(PWM->PWM_CR,0x0000005E);

//	time = 0x8000;	          
//	 while(time)
//  {    time--;  }

}


void DrvPiezo_Config2(void)
{
	uint32_t updrdy,smarst_rdy,time;
	
	WRITE_REG(PWM->PWM_CH0CG,0xFFFFFFFF);		
	WRITE_REG(PWM->PWM_CH1CG,0xFFFF01E1);		
	
	updrdy = READ_REG(PWM->PWM_STA)& 0x00010000; 
	while(updrdy != 0x00010000) {
		updrdy = READ_REG(PWM->PWM_STA) & 0x00010000;	
	}
	WRITE_REG(PWM->PWM_CR, 0x00010000);

		WRITE_REG(PWM->PZ0_CFG,0xFFFF0415);			 
		WRITE_REG(PWM->PZ1_CFG,0xFFFF0320);
	
		WRITE_REG(PWM->PWM_CR,0x0000001E);	
	
//	time = 0x8000;	          //15ms
//	 while(time)
//  {    time--;  }
	WRITE_REG(PWM->PWM_CR,0x00001E00);	

}


void DrvPwmDeinit(void) {
  CLEAR_BIT(RST_CTL_REG->MCU_BLK_RST, MCU_BLK_RST_PWM);
  SET_BIT(RST_CTL_REG->MCU_BLK_RST, MCU_BLK_RST_PWM);
}

void DrvPwmStart(void) {
  DrvPwmStartSma(PWM);
}

void DrvPwmStop(void) {
  DrvPwmStopSma(PWM);
}

/** @} PWM_DRV_Exported_Functions */
/** @} PWM_DRV */
/** @} GT98xx_DRV */
